package com.jgssp.demo;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.nativead.api.ATNative;
import com.anythink.nativead.api.ATNativeAdView;
import com.anythink.nativead.api.ATNativeDislikeListener;
import com.anythink.nativead.api.ATNativeEventListener;
import com.anythink.nativead.api.ATNativeNetworkListener;
import com.anythink.nativead.api.ATNativePrepareExInfo;
import com.anythink.nativead.api.ATNativePrepareInfo;
import com.anythink.nativead.api.NativeAd;

public class NativeActivity extends AppCompatActivity {

    ATNative atNatives;
    ATNativeAdView anyThinkNativeAdView;
    NativeAd mNativeAd;

    FrameLayout adContainer;
    private String TAG = "NativeActivity";
    private View mSelfRenderView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_native);
        adContainer = findViewById(R.id.fl_container);
        anyThinkNativeAdView = findViewById(R.id.native_ad_view);
        mSelfRenderView = findViewById(R.id.native_selfrender_view);
        initNativeAd();
        loadNativeAd();
    }

    public void initNativeAd() {
        //模板信息流ID
        String expressPlacementId = "b66f62cf76419a";
        //自渲染信息流ID
        String feedPlacementId = "b66f62d7d1116a";

        atNatives = new ATNative(this, feedPlacementId, new ATNativeNetworkListener() {

            @Override
            public void onNativeAdLoaded() {
                Log.i(TAG, "onNativeAdLoaded");
                showNativeAd();
            }

            @Override
            public void onNativeAdLoadFail(AdError adError) {
                //注意：禁止在此回调中执行广告的加载方法进行重试，否则会引起很多无用请求且可能会导致应用卡顿
                //AdError，请参考 https://docs.toponad.com/#/zh-cn/android/android_doc/android_test?id=aderror
                Log.i(TAG, "onNativeAdLoadFail:" + adError.getFullErrorInfo());
            }
        });
    }

    public void loadNativeAd() {

        if (atNatives != null) {
            atNatives.makeAdRequest();
        }
    }

    public void showNativeAd() {
        if (atNatives == null) {
            return;
        }


        NativeAd nativeAd = atNatives.getNativeAd();
        if (nativeAd != null) {
            mNativeAd = nativeAd;
            mNativeAd.setDislikeCallbackListener(new ATNativeDislikeListener() {
                @Override
                public void onAdCloseButtonClick(ATNativeAdView atNativeAdView, ATAdInfo atAdInfo) {
                    Log.i(TAG, "native ad onAdCloseButtonClick:\n");
                    adContainer.removeAllViews();

                }
            });
            mNativeAd.setNativeEventListener(new ATNativeEventListener() {
                @Override
                public void onAdImpressed(ATNativeAdView view, ATAdInfo atAdInfo) {
                    //ATAdInfo可区分广告平台以及获取广告平台的广告位ID等
                    //请参考 https://docs.toponad.com/#/zh-cn/android/android_doc/android_sdk_callback_access?id=callback_info

                    Log.i(TAG, "native ad onAdImpressed:\n");
                }

                @Override
                public void onAdClicked(ATNativeAdView view, ATAdInfo atAdInfo) {
                    Log.i(TAG, "native ad onAdClicked:\n");
                }

                @Override
                public void onAdVideoStart(ATNativeAdView view) {
                    Log.i(TAG, "native ad onAdVideoStart");
                }

                @Override
                public void onAdVideoEnd(ATNativeAdView view) {
                    Log.i(TAG, "native ad onAdVideoEnd");
                }

                @Override
                public void onAdVideoProgress(ATNativeAdView view, int progress) {
                    Log.i(TAG, "native ad onAdVideoProgress:" + progress);
                }
            });

            if (anyThinkNativeAdView != null) {
                anyThinkNativeAdView.removeAllViews();
            }

            ATNativePrepareInfo mNativePrepareInfo = null;

            try {
                mNativePrepareInfo = new ATNativePrepareExInfo();
                if (mNativeAd.isNativeExpress()) {
                    //模板渲染
                    mNativeAd.renderAdContainer(anyThinkNativeAdView, null);
                    mNativeAd.prepare(anyThinkNativeAdView, null);
                } else {
                    //自渲染
                    mSelfRenderView.setVisibility(View.VISIBLE);
                    SelfRenderViewUtil.bindSelfRenderView(NativeActivity.this, mNativeAd.getAdMaterial(), mSelfRenderView, mNativePrepareInfo);
                    mNativeAd.renderAdContainer(anyThinkNativeAdView, mSelfRenderView);
                    mNativeAd.prepare(anyThinkNativeAdView, mNativePrepareInfo);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mNativeAd != null) {
            mNativeAd.destory();
        }
    }

    @Override
    protected void onPause() {
        if (mNativeAd != null) {
            mNativeAd.onPause();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        if (mNativeAd != null) {
            mNativeAd.onResume();
        }
        super.onResume();
    }
}
